package com.c.cojbackendquestionservice.manager.impl;

import com.c.cojbackendcommon.common.AiQuestionConstant;
import com.c.cojbackendcommon.common.ErrorCode;
import com.c.cojbackendcommon.exception.BusinessException;
import com.c.cojbackendmodel.model.vo.AiQuestionVO;
import com.c.cojbackendquestionservice.manager.AiModel;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.lkeap.v20240522.LkeapClient;
import com.tencentcloudapi.lkeap.v20240522.models.ChatCompletionsRequest;
import com.tencentcloudapi.lkeap.v20240522.models.ChatCompletionsResponse;
import com.tencentcloudapi.lkeap.v20240522.models.Message;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class DeepSeek implements AiModel {
    private final String DEFAULT_MODEL = "deepseek-v3";

    // 使用腾讯云对接 DeepSeek AI
    @Resource
    private LkeapClient deepSeekClient;

    /**
     * 调用 AI 接口，获取响应字符串
     * 只要用户信息，在前端没有输入的情况下
     *
     * @param userPrompt
     * @return
     */
    public String doChat(String userPrompt) {
        return doChat("", userPrompt, DEFAULT_MODEL);
    }

    /**
     * 调用 AI 接口，获取响应字符串
     * 有系统预设以及用户信息
     *
     * @param systemPrompt
     * @param userPrompt
     * @return
     */
    public String doChat(String systemPrompt, String userPrompt) {
        return doChat(systemPrompt, userPrompt, DEFAULT_MODEL);
    }

    /**
     * 调用 AI 接口，获取响应字符串
     *
     * @param systemPrompt
     * @param userPrompt
     * @param model
     * @return
     */
    public String doChat(String systemPrompt, String userPrompt, String model) {
        // 构造消息列表
        try {
            // 实例化一个请求对象,每个接口都会对应一个request对象
            ChatCompletionsRequest req = new ChatCompletionsRequest();
            req.setModel(model);
            req.setStream(false);
            // 用户消息
            Message[] messages = new Message[2];
            Message message1 = new Message();
            message1.setRole("user");
            message1.setContent(userPrompt);
            messages[1] = message1;
            //系统消息
            Message message0 = new Message();
            message0.setRole("system");
            message0.setContent(systemPrompt);
            messages[0] = message0;
            req.setMessages(messages);
            // 返回的resp是一个ChatCompletionsResponse的实例，与请求对象对应
            ChatCompletionsResponse resp = deepSeekClient.ChatCompletions(req);
            //获取到输出结果
            String content = resp.getChoices()[0].getMessage().getContent();
            return content;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "AI 调用失败，没有返回结果");
        }
    }

    @Override
    public AiQuestionVO getGenResult(String title, String content, String language, Long questionId) {
        String promote = AiQuestionConstant.PRECONDITION + "标题 " + title + " \n内容: " + content + "\n编程语言: " + language;
        String resultData = doChat(promote);
        log.info("调用DeepSeek的AI 生成的信息: {}", resultData);
        String genResult = null;
        String genCode = resultData;
        if (resultData.split("【【【【【").length >= 3) {
            genResult = resultData.split("【【【【【")[1].trim();
            genCode = resultData.split("【【【【【")[2].trim();
        }
        return new AiQuestionVO(genResult, genCode, questionId);
    }
}
